let cartAllPrice = 0
let cartCheckedNumber = 0

$(function () {

    // var req = new XMLHttpRequest();
    // req.open('GET', document.location, false);
    // req.send(null);
    // console.log(req.getAllResponseHeaders());

    // console.log(getCookie('username'))
    // console.log(getCookie('goodsId'))
    // console.log(getCookie('goodsPrice'))
    $.ajax({
        url:'http://localhost:8080/order/addCart',
        type: 'post',
        data: {fkCartUserName:getCookie('username'),fkCartGoodId:getCookie('goodsId')
            ,cartGoodPrice:getCookie('goodsPrice'),stepTwoDetailId:getCookie('stepTwoDetailId')},
    }).then(res => {
        console.log(res.message)
        if(res.message == 'success'){

        } else {
            alert('页面访问出错')
        }

        $.ajax({
            url:'http://localhost:8080/order/show?username=' + getCookie('username'),
            type: 'get',
            dataType:'json'
        }).then(res => {
            allData = res.data
            console.log(allData)
            allData.forEach((v,k) => {
                console.log(v.cartGood.goodName)
            })
            cartInitialize()


        })
    }
    // ,function () {
    //     alert('401')
    //     window.location.href = 'http://localhost:8080'
    // }
    )


})
function initialize() {
    let allPrice = 0
    let allNumber = 0;
    allData.forEach((v,k) => {
        allPrice += v.cartGoodPrice
        allNumber++
    })
    $('#checkboxSelect').change(function(){
        // console.log($(this).prop('checked'))
        // console.log($('.oneCheckbox').length)
        for(let i = 0; i < $('.oneCheckbox').length; i++){
            $('.oneCheckbox').eq(i).prop('checked',$(this). prop('checked'))
        }
        console.log($(this).prop('checked'))
        if ($(this).prop('checked')){
            cartAllPrice = allPrice
            cartCheckedNumber = allNumber
            $('#cartCheckedNumber').text(allNumber)
            $('#cartAllPrice').text(allPrice)
        } else {
            cartAllPrice = 0
            cartCheckedNumber = 0
            $('#cartCheckedNumber').text(0)
            $('#cartAllPrice').text(0)
        }
    })

    let flagCheckbox = false
    for(let index = 0; index < $('.oneCheckbox').length; index++){
        $('.oneCheckbox').eq(index).change(function(){
            let temp = 0;
            for(let i = 0; i < $('.oneCheckbox').length; i++){
                // console.log($('.oneCheckbox').eq(i). prop('checked'))
                if($('.oneCheckbox').eq(i). prop('checked') == true){

                    $('.oneCheckbox').eq(i). prop('name','checkedyes')
                    temp++
                } else {
                    $('.oneCheckbox').eq(i). prop('name','checkedno')
                }
            }
            if(temp == $('.oneCheckbox').length){
                flagCheckbox = true
            } else {
                flagCheckbox = false
            }

            $('#checkboxSelect'). prop('checked',flagCheckbox)
            if($(this).prop('checked') == true) {
                // console.log(111)
                let onePrice = allData[allData.length - index - 1 + ''].cartGoodPrice
                cartAllPrice += onePrice
                cartCheckedNumber++
                $('#cartCheckedNumber').text(cartCheckedNumber)
                $('#cartAllPrice').text(cartAllPrice)
            } else {
                // console.log($('.oneCheckbox').eq(i).prop('checked'))
                let onePrice = allData[allData.length - index- 1 + ''].cartGoodPrice
                cartAllPrice -= onePrice
                cartCheckedNumber--
                $('#cartCheckedNumber').text(cartCheckedNumber)
                $('#cartAllPrice').text(cartAllPrice)
            }
            // console.log(temp)
            flagCheckbox = false
            // console.log($("input[name='checkedyes']").length)
        })

    }
}

function cartInitialize() {
    let cartHtml = ''

    allData.forEach((v,k) => {

        cartHtml += `
        <tr>
            <th width="5%" height="35"><input class="oneCheckbox" type="checkbox"/></th>
            
            <td height="95" align="center">
                <div class="contact_way_img" style="width: 50px; height: 50px"><img height="50" width="50" src="http://localhost/images/${v.cartGood.goodImg}"></div>
                <div class="contact_way_txt"><p><span>${v.cartGood.goodName}</span></p></div>
                <div class="clear"></div>
            </td>
            
            <td align="center" style="line-height:22px;">
                <h3 class="priceCartOne">￥${v.cartGoodPrice}</h3>
            </td>
                
            <td style="padding-left:28px; line-height: 18px;text-align: left;">
                <span>
                |&nbsp;&nbsp;
            `
            v.tbInfoDetailList.forEach((v,k) => {
                cartHtml += `
                    <a>${v.infoDetailName}</a>
                    |&nbsp;&nbsp;
                 `
            })
            cartHtml += `
            </td>
        </tr>
        `
        $('#cart_info').prepend(cartHtml)
        cartHtml = ''
    })
    let hardHtml = `
            <tr>
                <th width="5%" height="35">全选<input id="checkboxSelect" type="checkbox"/></th>
                <th width="20%" height="35">型号</th>
                <th width="16%">回购价格</th>
                <th width="64%">评估描述情况</th>
            </tr>
    `
    $('#cart_info').prepend(hardHtml)
    initialize()
}